/* 
 * File:   EventSystem.cpp
 * Author: aljosa
 * 
 * Created on April 28, 2010, 8:19 PM
 */

#include "EventSystem.h"

namespace xEngine
{
    namespace CoreSystem
    {
        namespace Events
        {
            // Only instance
            EventSystem EventSystem::instance;

            //
            // Constructors, destructors
            //
            EventSystem::EventSystem() {

            }


            EventSystem::~EventSystem() {
            }

            void EventSystem::init() {
                // Set user event handler to NULL
                this->handleUserEvents = NULL;
            }

            void EventSystem::shutDown() {
                // Delete non-autoreleased events
                
            }

            //
            // Queue access
            //
            Event* EventSystem::popEvent() {
                // Get event
                Event *event = this->events.front(); // Get first event
                //this->events.pop(); // Take event from the queue
                this->events.erase( events.begin() );

                return event;
            }

            void EventSystem::pushEvent(Event *event) {
                // Add event to the queue
                this->events.push_back(event);  //push(event);
            }

            void EventSystem::print() {
                list<Event*>::iterator it;

                for ( it = events.begin(); it != events.end(); it++ )
                    (*it)->print();
            }

            //
            // Event handling
            //
         /*   bool EventSystem::handleEvent(Event *event) {

                bool handled; // Event status
                if ( this->handleUserEvents != NULL ) { // Do we have user (custom) event handler?
                    handled = handleUserEvents(event); // Handle with custom event handle func.
                }
                
                if ( !handled ) // Has event been handled yet?
                    handled = handleEngineEvents(event); // No - Handle it

                return handled; // Return handle status
            }

            // Engine (low-level) events handling
            bool EventSystem::handleEngineEvents(Event *event) { // Engine events handler
                
            }

            // User-specified handler setter
            void EventSystem::setUserEventsHandler(bool (*handle)(Event*)) {
                this->handleUserEvents = handle; // Set pointer to function
            }*/


          /*  queue<Event*> *EventSystem::getQueue() {
                return &this->events;
            }*/

            int EventSystem::numPending() const {
                return (int)this->events.size();
            }


            //
            // Singleton
            //
            EventSystem &EventSystem::getInstance() {
                return instance;
            }
        }
    }
}
